#include <fstream>
#include <iostream>

using namespace std;

int main ( int argc, char *argv[] )
{
  const int arraySizeIncrease = 1000000;
  int arraySize = arraySizeIncrease, wordCount = 0;
  char** wordListArray = new char* [arraySize];
  char wordSize [512];
  if ( argc != 3 ) {
	cout<<"usage: "<< argv[0] <<" <input_filename> <output_filename>\n";
	abort();
  }
  else {
    ifstream T ( argv[1] );
	ofstream fileOutputStreamTemp ("parseSms");
	char ch;
	while(!T.eof()){
		T.get(ch);
		int  newkey   = ch;
		char ASCII    = newkey & 0xff;
		if((ASCII >= 48 && ASCII <= 57) || (ASCII >= 65 && ASCII <= 90) || (ASCII >= 97 && ASCII <= 122))
			fileOutputStreamTemp << ch;
		else
			fileOutputStreamTemp << "\t";
	}
	fileOutputStreamTemp.close();
    T.close();
	ifstream the_file ( "parseSms"  );
    if ( !the_file.is_open() )
		cout<<"Could not open file\n";
    else {
		while (the_file >> wordSize )
		{
			wordListArray[wordCount] = new char [strlen (wordSize) + 1];
			strcpy (wordListArray[wordCount], wordSize);
			wordCount++;
			//If the File has more then 1000000 Words!
			if (wordCount % arraySizeIncrease == 0)
			{
				char** oldSize = wordListArray;
				arraySize += arraySizeIncrease;
				wordListArray = new char* [arraySize];
				memcpy (wordListArray, oldSize, wordCount * sizeof(char*));
				delete oldSize;
			}
		}
	}
	the_file.close();
  }
  	ofstream fileOutputStream (argv[2]);
	for (int cntIndex = 4; cntIndex < wordCount; cntIndex+=8)
	{
		for (int cntIndexIn = cntIndex; cntIndexIn < cntIndex+8; cntIndexIn++)
		{
			fileOutputStream << wordListArray[cntIndexIn] << ",";
		}
		fileOutputStream  << "\n";
	}
	fileOutputStream.close();

	for (int cntIndexN = 0; cntIndexN < wordCount; cntIndexN++)
	{
		delete wordListArray[cntIndexN];
	}
	delete wordListArray;
	wordListArray = NULL;
	cout << "\nOutput file Written.\n\n";
}

